package com.vitabuybuy.tv.ui.mine.coupons

import android.os.Bundle
import com.google.android.material.tabs.TabLayout
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityMycouponsBinding
import com.vitabuybuy.tv.dto.MyCouponDto
import com.vitabuybuy.tv.ui.mine.coupons.viewmodel.MyCouponsViewModel
import com.vitabuybuy.tv.ui.shopcenter.CateSearchGoodsResultActivity
import kotlinx.android.synthetic.main.activity_mycoupons.*

class MyCouponsActivity : BaseActivity<MyCouponsViewModel, ActivityMycouponsBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_mycoupons

    private var pageNumber = 1
    private var status = ""

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.my_coupons))
        mBinding.viewModel = viewModel
        tlClassification.apply {
            addTab(
                tlClassification.newTab().setText(getString(R.string.all))
                    .setTag(getString(R.string.all))
            )
            addTab(
                tlClassification.newTab().setText(getString(R.string.not_use))
                    .setTag(getString(R.string.not_use))
            )
            addTab(
                tlClassification.newTab().setText(getString(R.string.used))
                    .setTag(getString(R.string.used))
            )
            addTab(
                tlClassification.newTab().setText(getString(R.string.expired))
                    .setTag(getString(R.string.expired))
            )
        }
    }

    override fun observeData() {
        viewModel.getCouponList(pageNumber, status)
    }

    override fun eventHandling() {
        tlClassification.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                pageNumber = 1
                status =
                    if (tab?.tag as String == getString(R.string.all)) "" else tab.tag as String
                viewModel.getCouponList(pageNumber, status)
            }

            override fun onTabReselected(p0: TabLayout.Tab?) {
            }

            override fun onTabUnselected(p0: TabLayout.Tab?) {
            }
        })
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber++
                viewModel.getCouponList(pageNumber, status)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber = 1
                viewModel.getCouponList(pageNumber, status)
            }
        })
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            0 -> {
                pageNumber = 1
                viewModel.getCouponList(pageNumber, status)
            }
            1->{
                refresh.onLoad(msg.arg1)
            }
            2 -> {
                val item = msg.obj as MyCouponDto
                CateSearchGoodsResultActivity.openActivity(mContext,
                null,null,item.coupon.id,getString(R.string.coupons_commend))
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>) {
            mContext.startActivity(null, MyCouponsActivity::class.java)
        }
    }
}
